#include "fx2.h"
#include "fx2regs.h"
#include "syncdly.h"
#include "ov5647.h"
#include "sccb.h"
/*厂商指令*/
#define VR_NEW_FRAME			0XFF	/* 从当前前前帧停止采集集*/
#define VR_USB_VERSION			0XFE	/* USB H版本*/  
#define VR_MAX_PKT_SIZE			0XFD	/* 获取包大夏小 */  
#define VR_REG_READ 	 		0xFC	/* 读取OV5647存器地址*/
#define VR_REG_WRITE 			0xFB	/* 向OV5647寄器写入数据 */
#define VR_MARKER_PKT_SIZE		0XFA	/* get marker packet size */
#define VR_MARKER_SIZE			0XF9	/* get aactualmarker size */
#define VR_MARKER				0XF8	/* geeeet t actual marker id */
#define VR_BYTE_WORD			0XF7	/* 设置总线位宽 8或者16位 */
#define VR_FIRMWRITE			0XF6	/* 向FX2固件的PROM写入一个字节数据*/
#define VR_FIRMREAD				0XF5	/* 从FX2的EPRM读取一个字节数据 */
#define VR_PIXCLK_CONFIG		0XF4	/* PCLK上升沿采集数还是下降沿采集数据*/
#define VR_POWER_SENSOR			0xF3	/* 图像传感慨器电源控制 */
#define VR_REG_READ_X			0XF2	/* 顺序读取X字节数 */
#define VR_REG_WRITE_X 			0XF1	/* 顺序写入x字节数据 */
#define VR_RESET				0XF0	/* 硬件复位传到默认状态*/
#define VR_I2C_CTL 				0XEF	/* 获取I2C总线状*/
#define VR_GET_I2C				0XEE	/* 获取总线状态结果*/
#define VR_INTMEM_READ_X		0XED	/* 从FX2的xdata存区读取一字节数据*/
#define VR_INTMEM_WRITE_X		0XEC	/* 写一个字节数据到fx的xdata储存区*/
#define VR_INTMEM_READ			0XEB	/* 从内部储存区读取数据*/
#define VR_INTMEM_WRITE			0XEA	/* 写入数据到内部储存区*/
#define VR_SFR_READ				0XE9	/* 读特功能寄存器*/
#define VR_SFR_WRITE			0XE8	/* 写特功能寄存器*/
#define VR_REG_ADD 				0XE7	/* 添加指指指令到读-写据指令索引表*/
#define VR_JTAG_IO				0XE6	/* */       
#define VR_JTAG_ON 				0XE5	/* */       
#define VR_JTAG_OFF 			0XE4	/* */       
#define VR_JTAG_GET 			0XE3	/* */        
#define VR_I2C_READ_16 			0XE2	/* */         
#define VR_I2C_WRITE_16_16		0XE1	/* */          
#define VR_I2C_WRITE_16_8		0XE0	/* */         
#define VR_I2C_READ_16_8 		0XDF	/* */         
#define VR_I2C_READ_X 			0xDE	/* */  
#define VR_GET_SCLK				0XDD	/* 获取SCLK的频率*/

#define VR_SET_GAIN				0XDC	/* 设置全局增益 */
#define	VR_SET_GAIN_R			0xDB	/* 设置红色增益 */		
#define VR_SET_GAIN_G			0xDA	/* 设置绿色增益 */
#define VR_SET_GAIN_B			0XD9	/* 设置蓝色增益 */
#define VR_SET_SHUTTER			0XD8	/* 设置曝光时间 */

#define VR_GET_GAIN				0XD7	/* 获取全局增益 */
#define	VR_GET_GAIN_R			0xD6	/* 获取红色增益 */
#define VR_GET_GAIN_G			0xD5	/* 获取绿色增益 */
#define VR_GET_GAIN_B			0XD4	/* 获取蓝色增益 */
#define VR_GET_SHUTTER			0XD3	/* 获取曝光时间 */

   
/*厂商指令*/
BOOL DR_VendorCmnd(void)
{
	
	WORD regAddr;
	BYTE regVal;
	BYTE length;
	WORD temp;
	int i;
   switch(SETUPDAT[1])
   {	   
		/* 读取寄存器 */
		case VR_REG_READ:		
			{
			regAddr=SETUPDAT[4];
			regAddr|=SETUPDAT[5]<<8;
			regVal=OV5647_reg_read(regAddr);
			*EP0BUF = regVal;
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; // Acknowledge handshake phase of device request
			
			break;
			}
		/* 写入寄存器 */
		case VR_REG_WRITE:	
			{
			regAddr=SETUPDAT[4];
			regAddr|=SETUPDAT[5]<<8;
				
			regVal=SETUPDAT[2];
			OV5647_reg_write(regAddr,regVal);
			
			*EP0BUF = regVal;
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;
			}	
		/* 连续读出寄存器值 */	
		case VR_REG_READ_X :	
		{	
			regAddr=SETUPDAT[4];				
			regAddr|=SETUPDAT[5]<<8;
			length = SETUPDAT[6];
		
		for(i=0;i<length;i++)
			{
			EP0BUF[i]=OV5647_reg_read(regAddr);
			regAddr++;				
			}
			EP0BCH = 0;
			EP0BCL = length; 
			EP0CS |= bmHSNAK; 	
			break;	
			
		}
		/* 连续写入寄存器值 */			
		case VR_REG_WRITE_X :
		{						
			regAddr=SETUPDAT[4];				
			regAddr|=SETUPDAT[5]<<8;
			length = SETUPDAT[6];
		
		for(i=0;i<length;i++)
			{
			OV5647_reg_write(regAddr,EP0BUF[i]);
			regAddr++;				
			}
						EP0BCH = 0;
			EP0BCL = length; 
			EP0CS |= bmHSNAK; 	
			break;				
		}

		/* 获取图像传感器增益 */
		case VR_GET_GAIN :
		{
			
			*EP0BUF = OV5647_get_gain16();
			EP0BCH = 0;
			EP0BCL = 2; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 获取图像传感器增益 */
		case VR_GET_GAIN_R :
		{
			
			//*EP0BUF = ;
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 获取绿色增益 */
		case VR_GET_GAIN_G :
		{
			
			//*EP0BUF = ;
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 获取蓝色增益 */
		case VR_GET_GAIN_B :
		{
			
			//*EP0BUF = ;
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 获取曝光时间 */
		case VR_GET_SHUTTER :
		{
			
			//*EP0BUF = OV5647_get_shutter();
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}


		/* 设置图像传感器增益 */
		case VR_SET_GAIN :
		{
			
			temp=EP0BUF[0];
			EP0BCH = 0;
			EP0BCL = 2; 
			EP0CS |= bmHSNAK;

			OV5647_set_gain16(); 	
			break;	

		}

		/* 设置图像传感器红色增益 */
		case VR_SET_GAIN_R :
		{
			
			*EP0BUF = OV5647_get_SCLK(0x48);
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 设置绿色增益 */
		case VR_SET_GAIN_G :
		{
			
			*EP0BUF = OV5647_get_SCLK(0x48);
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 设置蓝色增益 */
		case VR_SET_GAIN_B :
		{
			
			*EP0BUF = OV5647_get_SCLK(0x48);
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}

		/* 设置曝光时间 */
		case VR_SET_SHUTTER :
		{

			temp=SETUPDAT[4];
			temp|=SETUPDAT[5]<<8;			
			OV5647_set_shutter(temp);	
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;	

		}


		/*获取图像传感器的系统时钟频率*/
		case VR_GET_SCLK :	
		{						
			*EP0BUF = OV5647_get_SCLK(0x48);
			EP0BCH = 0;
			EP0BCL = 1; 
			EP0CS |= bmHSNAK; 	
			break;				
		}		
		
		default:
		// error, no such vendor request			
		return(TRUE); 
   
   }
   return(FALSE);
}